package com.winkyle.woodenfish.controller.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.os.Bundle;

import com.winkyle.woodenfish.R;
import com.winkyle.woodenfish.databinding.ActivityPersonalDataBinding;
import com.winkyle.woodenfish.entity.UserInfo;
import com.winkyle.woodenfish.factory.presenterfactory.CreatePresenter;
import com.winkyle.woodenfish.mvpview.PersonalDataView;
import com.winkyle.woodenfish.presenter.PersonalDataPresenter;
import com.winkyle.woodenfish.util.ImagePickerUtil;

/**
 * @author winkyle
 * @date 2018/06/13
 * @description 继承自 {@link AbstractMvpActivity}的活动类PersonalDataActivity，app的个人资料页
 */
@CreatePresenter(value = PersonalDataPresenter.class)
public class PersonalDataActivity  extends AbstractMvpActivity<PersonalDataView,PersonalDataPresenter> implements PersonalDataView {
    private String TAG = "PersonalDataActivity";

    private ActivityPersonalDataBinding activityPersonalDataBinding;
    /**
     * @description 活动创建，指定layout布局
     * @param savedInstanceState 保存的活动状态的数据
     * @return
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityPersonalDataBinding = DataBindingUtil.setContentView(this,R.layout.activity_personal_data);
        initActivity();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getMvpPresenter().viewPersonalData();
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        imagePickerUtil.onActivityResult(this, requestCode, resultCode, data);
    }

    @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                                     @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        imagePickerUtil.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }

    private boolean isLogin(){
        return true;
    }

    private ImagePickerUtil imagePickerUtil;
    /**
     * @description 初始化活动的一些数据和动作
     * @return
     */
    public void initActivity(){
        activityPersonalDataBinding.setPersonalDataView(this);
        activityPersonalDataBinding.swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getMvpPresenter().viewPersonalData();
            }
        });

        imagePickerUtil = new ImagePickerUtil("选择头像");
        //设置可裁剪
        imagePickerUtil.setCropImage(true);
        imagePickerUtil.setImageCropListener(new ImagePickerUtil.ImageCropListener() {

            @Override
            public void imageCropSucceed(Uri imageUri) {
                getMvpPresenter().changeHeadPortrait(imagePickerUtil.getFile());
            }
        });
        imagePickerUtil.setPickeOneImage();
    }


    @Override
    public void refreshPersonalData(UserInfo userInfo) {
        if (activityPersonalDataBinding.swipeRefresh.isRefreshing()){
            activityPersonalDataBinding.swipeRefresh.setRefreshing(false);
        }
        activityPersonalDataBinding.setUserInfo(userInfo);
    }

    @Override
    public void changeHeadPortrait() {
        imagePickerUtil.startChooser(this);
    }

    @Override
    public void changeHeadPortraitSucceed(String imageUrl) {
        UserInfo userInfo = activityPersonalDataBinding.getUserInfo();
        userInfo.setHeadPortrait(imageUrl);
        activityPersonalDataBinding.setUserInfo(userInfo);
    }
}
